/**
 * 
 */
package cn.edu.pku.sei.ra.guineapig.rss.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.pku.sei.ra.guineapig.rss.intf.ItemArtist;
import cn.edu.pku.sei.ra.guineapig.rss.intf.ItemArtistHome;
import cn.edu.pku.sei.ra.guineapig.rss.intf.ItemInfor;



/**
 * @author nishimura
 *
 */
public class MainServlet extends HttpServlet {

	PrintWriter out=null;
	ItemArtist artist=null;
	/**
	 * 
	 */
	public MainServlet() {
		// TODO Auto-generated constructor stub
	}
	

	protected void doGet(HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException,
    ServletException {
        dispatch(pRequest, pResponse);
    }

    /**
     * Response to the POST request.
     * @param pRequest See HttpServlet
     * @param pResponse See HttpServlet
     * @throws IOException
     * @throws ServletException
     */
    protected void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException,
    ServletException {
        dispatch(pRequest, pResponse);
    }

    /**
     * Dispatch the response.
     * @param pRequest Request
     * @param pResponse Response
     * @throws IOException
     */
    protected void dispatch(HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException {
    	try {
			InitialContext ic=new InitialContext();
			Object obj=ic.lookup("java:comp/env/ejb/rssartist");
			ItemArtistHome home=(ItemArtistHome)PortableRemoteObject.narrow(obj,ItemArtistHome.class);
			artist=home.create();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//Catalog
    	//"java:comp/env/ejb/User" 
    	pResponse.setContentType("application/rss+xml");
         out = pResponse.getWriter();
         out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
         out.println("<rss version=\"2.0\">");
         out.println("<channel>");
         out.println("<title>"+artist.getMainTitle()+"</title>");
         out.println("<link>http://www.w3schools.com</link>");
         ItemInfor[] res=artist.getItems();
         for(int i=0;i<res.length;i++){
        	 writeItem(res[i]);
         }
         
         out.println("</channel>");
         out.println("</rss>");
    }
    
    private void writeItem(String title, String link, String description){
    	out.println("<item>");
    	out.println(String.format("<title>%s</title>", title));
    	out.println(String.format("<link>%s</link>", link));
    	out.println(String.format("<description>%s</description>", description));
    	out.println("</item>");    	
    }
    
    private void writeItem(ItemInfor item){
    	writeItem(item.title,item.link,item.desc);
    }
}


